package com.noble.meether.fragment.user;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.view.base.BasePanel;
import com.noble.library.widget.RecyclerView.base.BaseRecyclerViewHolder;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.fragment.user.bean.UserAuth;

import java.util.ArrayList;
import java.util.List;

/**
 * 个人中心的list布局panel
 */
public class UserFragmentFootPanel extends BasePanel implements BaseRecyclerViewHolder.OnItemClickListener {

    private RecyclerView recyclerView;
    private UserAuthAdapter userAuthAdapter;

    public UserFragmentFootPanel(Context context) {
        super(context);
        setContentView(R.layout.p1_user_fragment_footer);
        initData();
    }

    // 初始化条目数据
    private void initData() {
        List<UserAuth> userAuthList = new ArrayList<>();
        Resources resources = getContext().getResources();
        recyclerView = (RecyclerView) findViewById(R.id.user_fragment_footer);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setNestedScrollingEnabled(false); // 嵌套ScrollView滑动惯性消失
        String[] names = resources.getStringArray(R.array.user_authority_name);
        int[] levels = resources.getIntArray(R.array.user_authority_level);
        int[] icons = new int[]{R.drawable.user_wallet, R.drawable.user_wallet, R.drawable.user_vip, R.drawable.user_invite, R.drawable.user_gift, R.drawable.user_suggest};

        for (int i = 0; i < names.length; i++) {
            UserAuth userAuth = new UserAuth(i, icons[i], levels[i], names[i], isShow(i));
            userAuthList.add(userAuth);
        }
        userAuthAdapter = new UserAuthAdapter();
        userAuthAdapter.setList(userAuthList);
        recyclerView.setAdapter(userAuthAdapter);
        userAuthAdapter.setOnItemClickListener(this);
    }

    /**
     * 刷新list
     */
    public void refreshView() {
        userAuthAdapter.notifyDataSetChanged();
    }

    /**
     * 判断指定条目显隐状态
     */
    private boolean isShow(int id) {
        boolean isMan = ModuleMgr.getCenterMgr().getMyInfo().isMan();
        if (isMan) {
            return id != CenterItemID.i_Center_item_1 && id != CenterItemID.i_Center_item_5;
        }
        return id != CenterItemID.i_Center_item_2;
    }

    @Override
    public void onItemClick(View convertView, int position) {
        switch (position) {
            case CenterItemID.i_Center_item_1:              // 女性: 我的收益
                MeetUIShow.showMyWalletAct(getContext());
                break;

            case CenterItemID.i_Center_item_2:              // 男性： 购买钻石
                MeetUIShow.showMyDiamondsAct(getContext());
                break;

            case CenterItemID.i_Center_item_3:
                MeetUIShow.showMyVipAct(getContext());
                break;

            case CenterItemID.i_Center_item_4:
                MeetUIShow.showInviteAct(getContext());
                break;

            case CenterItemID.i_Center_item_5:  // 我的礼物
                UIShow.showMyGiftActivity(getContext());
                break;

            case CenterItemID.i_Center_item_6:
                MeetUIShow.showSuggestAct(getContext());
//                // TODO test
//                HashMap<String, Object> param = new HashMap<>();
//                param.put(EditKey.s_key_statusAuth, -3);
//                ModuleMgr.getCenterMgr().updateMyInfo(false, param, null);
                break;
        }
    }
}
